# -*- encoding=utf8 -*-
__author__ = "lambdax"

from airtest.core.api import *

auto_setup(__file__)
# -*- encoding=utf8 -*-
__author__ = "lambdax"

from airtest.core.api import *

auto_setup(__file__)



from poco.drivers.android.uiautomation import AndroidUiautomationPoco
poco = AndroidUiautomationPoco(use_airtest_input=True, screenshot_each_action=False)

def swp():
    swipe((400,400), vector=[0, -0.3324])

    
def get_menu_down():
        ret = exists(Template(r"tpl1624951273002.png",threshold=0.9,  record_pos=(0.446, -0.149), resolution=(1080, 2160)))
        if ret and ret[0] > 980:
            return ret
        return False
# swp()

def to_det(fa_point):
    try:
        ret = get_menu_down()
        if ret:
            print("找到下拉点：",ret)
            if fa_point[1] > ret[1]:
    #             执行删除操作
                touch(ret)
                sleep(2)
                touch(Template(r"tpl1624951517646.png", record_pos=(-0.263, 0.74), resolution=(1080, 2160)))
                sleep(2)
                touch(Template(r"tpl1624951612969.png", record_pos=(0.205, 0.158), resolution=(1080, 2160)))

        else:
            print("没有找到下拉点")

    except:
        print("没有找到下拉点")


def work():
    try:
        ret = assert_exists(Template(r"tpl1624951105926.png", record_pos=(0.356, 0.867), resolution=(1080, 2160)), "查看结果")
        to_det(ret)
        print("找到结果点：",ret)
    except:
        print("没有找到")


def to_delP(obj):
    sleep(1)
    ret = None
    ret = get_menu_down()
    if not ret:
        swipe((400,400), vector=[0, 0.1])
        while 1:
            print("没有下拉框")
            ret = get_menu_down()
            if not ret:
                swipe((400,400), vector=[0, 0.1])
            else:
                touch(ret)
                break
    else:
        obj.focus((0.99,0.01)).click()
    
    sleep(1)
    swipe((400,400), vector=[0, -0.1324])
    
    sleep(1)
    touch(Template(r"tpl1624951517646.png", record_pos=(-0.263, 0.74), resolution=(1080, 2160)))
    sleep(0.1)
    touch(Template(r"tpl1624951612969.png", record_pos=(0.205, 0.158), resolution=(1080, 2160)))


def workP():
    ret = False
    objs = poco("android:id/content").child("android.widget.RelativeLayout").child("android.view.ViewGroup").offspring("com.sina.weibo:id/lv_content").child("android.widget.RelativeLayout")
    for obj in objs:
        lbt = obj.offspring('com.sina.weibo:id/leftButton')
        if lbt.exists() and lbt.attr('enabled') == False:
#             todo delete
            print(lbt.attr('enabled'))
            to_delP(obj)
            ret = True
            break
    
        lbt = obj.offspring('android.widget.Button')
        if lbt.exists() and lbt.attr('text') == "查看结果":
#             todo delete
# 检查位置是否在下
            ret = exists(Template(r"tpl1624951105926.png", record_pos=(0.356, 0.867), resolution=(1080, 2160)))
            if ret and ret[1] > 1733:
                print(lbt.attr('text'))
                to_delP(obj)
                ret = True
                break
            else:
                swipe((400,400), vector=[0, 0.1])
                sleep(0.5)
                ret = True
                break
    return ret
    



def loop():
    ret= False
    while 1:
        try:
            ret = workP()
        except:
            pass
        if not ret:
            swp()
        
loop()
